<script setup lang="ts">
// 引入组件
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
// 引入api
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import type { XtxGuessInstance } from '@/types/component'
import { useGuessList } from '@/composables'

// 调用请求函数

// 给ref指定数据类型
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  console.log(res)
  // 类型问题
  bannerList.value = res.result
}

// 获取前台接口的函数
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  console.log(res)
  categoryList.value = res.result
}

// 获取热门推荐的商品list列表
const getHomeHotList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  console.log(res)
  getHomeHotList.value = res.result
}
// 猜你喜欢的的请求函数,需要在组件中才可以
// const getHomeGoodsGuessLikeData = async () => {
//   const res = await getHomeGoodsGuessLikeData()
//   console.log(res)
// }
// 获取猜你喜欢实例
// const guessRef = ref<XtxGuessInstance>()
// // 滚动触底事件,导入触底事件
// const onScrolltolower = () => {
//   guessRef.value?.getMore()
// }
// 猜你喜欢的组合式函数调用
const { guessRef, onScrolltolower } = useGuessList()
// 设置下拉状态
const isTriggered = ref(false)
const onRefresherrefresh = async () => {
  isTriggered.value = true
  // 重置猜你喜欢的组件数据
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭下拉刷新的动画
  isTriggered.value = false
}
// 设置加载中标记
const isLoading = ref(false)
// 在加载前进行调用
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isLoading.value = false
})
</script>

<template>
  <!-- 自定义导航栏区域 -->
  <CustomNavbar></CustomNavbar>
  <!-- 定义滚动区域使用<scroll-view scroll-x> -->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    @scrolltolower="onScrolltolower"
    :refresher-triggered="isTriggered"
    class="scroll-view"
    scroll-y
  >
    <!--刷新等待骨架屏幕区域 -->
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <template v-else>
      <!-- 自定义轮播图区域-->
      <!-- 父传子 -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 商品分类面板 -->
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <!-- 热门推荐 -->
      <!-- 父传子 -->
      <HotPanel :list="getHomeHotList"></HotPanel>
      <!-- 猜你喜欢页面渲染 需要进行类型配置在compent.ts中进行-->
      <!-- 子传父 -->
      <XtxGuess ref="guessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
